package net.mikoo.seals.biz.manager;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import net.mikoo.seals.biz.mapper.ApplyMapper;
import net.mikoo.seals.biz.mapper.ApplyVerifyMapper;
import net.mikoo.seals.biz.mapper.CustosMapper;
import net.mikoo.seals.biz.mapper.DevicesActMapper;
import net.mikoo.seals.biz.mapper.DevicesMapper;
import net.mikoo.seals.biz.mapper.DevicesTakeMapper;
import net.mikoo.seals.biz.mapper.PartyMapper;
import net.mikoo.seals.biz.mapper.SealMapper;
import net.mikoo.seals.biz.mapper.WorkflowMapper;
import net.mikoo.seals.common.enums.ApplyStatusEnum;
import net.mikoo.seals.common.enums.ApplyTypeEnum;
import net.mikoo.seals.common.enums.CustosStatusEnum;
import net.mikoo.seals.common.enums.CustosTypeEnum;
import net.mikoo.seals.common.enums.TakeStatusEnum;
import net.mikoo.seals.common.enums.VerifyStatusEnum;
import net.mikoo.seals.common.enums.WorkflowTypeEnum;
import net.mikoo.seals.common.exception.DataNotFoundException;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.model.Apply;
import net.mikoo.seals.model.ApplyVerify;
import net.mikoo.seals.model.Custos;
import net.mikoo.seals.model.Devices;
import net.mikoo.seals.model.DevicesAct;
import net.mikoo.seals.model.DevicesTake;
import net.mikoo.seals.model.Party;
import net.mikoo.seals.model.Seal;
import net.mikoo.seals.model.Workflow;

@Component
public class VerifyManager {
	
	@Autowired
	private ApplyMapper applyMapper;
	
	@Autowired
	private NoticeManager noticeManager;
	
	@Autowired
	private ApplyVerifyMapper applyVerifyMapper;
	
	@Autowired
	private DevicesActMapper devicesActMapper;
	
	@Autowired
	private DevicesMapper devicesMapper;
	
	@Autowired
	private DevicesTakeMapper devicesTakeMapper;
	
	@Autowired
	private WorkflowMapper workflowMapper;
	
	@Autowired
	private SealMapper sealMapper;
	
	@Autowired
	private CustosMapper custosMapper;
	
	@Autowired
	private PartyMapper partyMapper;
	
	public void checkParams(ApplyVerify verify) {
		
		if(verify == null) {
			throw new ParametersException("verify");
		}
		
		if(verify.getApplyVerifyId() == null) {
			throw new ParametersException("ApplyVerifyI is null");
		}
		
		if(verify.getPartyId() == null) {
			throw new ParametersException("PartyId is null");
		}
		
		if(verify.getCompanyId() == null) {
			throw new ParametersException("CompanyId is null");
		}
		
		if(StringUtils.isBlank(verify.getVerifyStatus())) {
			throw new ParametersException("请选择审核状态");
		}
		
	}

	public void verify(ApplyVerify verify) {
		
		ApplyVerify existVerify = applyVerifyMapper.selectInfoById(verify.getApplyVerifyId());
		if(existVerify == null) {
			throw new ParametersException("审核信息不存在");
		}
		
		if(!verify.getPartyId().equals(existVerify.getPartyId())) {
			throw new ParametersException("不是当前审核人，无权审核");
		}
		
		if(!verify.getCompanyId().equals(existVerify.getCompanyId())) {
			throw new ParametersException("不是当前公司员工，无权审核");
		}
		
		if(!existVerify.getVerifyStatus().equals(VerifyStatusEnum.PENDING.code())) {
			throw new ParametersException("当前任务不是待审核");
		}
		Apply apply = applyMapper.selectInfoById(existVerify.getApplyId());
	
		verify.setVerifyTime(new Date());
		existVerify.setVerifyResult(verify.getVerifyResult());
		existVerify.setRemark(verify.getRemark());
		existVerify.setVerifyTime(new Date());
		existVerify.setVerifyStatus(verify.getVerifyStatus());
		applyVerifyMapper.updateChangeStatus(verify);
		
		//拒绝通过
		if(verify.getVerifyStatus().equals(VerifyStatusEnum.REJECT.code())) {
			apply.setVerifyResult(verify.getVerifyResult());
			apply.setApplyStatus(ApplyStatusEnum.REJECT.code());
			apply.setVerifyEndTime(new Date());
			applyMapper.updateChangeStatus(apply);
			noticeManager.verify(apply, apply.getSeal(), existVerify);
		//审核通过	
		}else {
			verifyUsedApply(apply,existVerify);
		}
	}
	
	private void verifyUsedApply(Apply apply,ApplyVerify verify) {
		Seal seal = sealMapper.selectInfoById(apply.getSealId());
		if(seal == null) {
			throw new DataNotFoundException("申请的印章不存在");
		}
		
		
		
		String useSpecial = apply.getUseSpecial();
		//判断是否是特殊申请
		if(useSpecial.equals("Y")) {
			apply.setVerifyResult(verify.getVerifyResult());
			apply.setApplyStatus(ApplyStatusEnum.PASSED.code());
			apply.setVerifyEndTime(new Date());
			applyMapper.updateChangeStatus(apply);
			if(apply.getApplyType().equals(ApplyTypeEnum.TAKE_APPLY.code())) {
				addTakeInfo(apply,seal);
			}else if(apply.getApplyType().equals(ApplyTypeEnum.USED_APPLY.code())) {
				addDevicesAct(apply);
			}
			noticeManager.verify(apply, seal, verify);
		}else {
			Long workflowId = seal.getWorkflowId();
			Workflow workflow = workflowMapper.selectInfoById(workflowId);
			if(workflow.getWorkflowType().equals(WorkflowTypeEnum.JOIN.code())){
				List<ApplyVerify> verifys = applyVerifyMapper.selectHaveVerifyByCurrent(verify);
				if(verifys == null || verifys.isEmpty()) {
					apply.setVerifyResult(verify.getVerifyResult());
					apply.setApplyStatus(ApplyStatusEnum.PASSED.code());
					apply.setVerifyEndTime(new Date());
					applyMapper.updateChangeStatus(apply);
					//3.添加使用记录
					if(apply.getApplyType().equals(ApplyTypeEnum.TAKE_APPLY.code())) {
						addTakeInfo(apply,seal);
					}else if(apply.getApplyType().equals(ApplyTypeEnum.USED_APPLY.code())) {
						addDevicesAct(apply);
					}
					noticeManager.verify(apply, seal, verify);
				}
			}else if(workflow.getWorkflowType().equals(WorkflowTypeEnum.QUEUE.code())) {
				//1.变更审核状态
				ApplyVerify nextVerify = applyVerifyMapper.selectNextVerifyByCurrent(verify);
				if(nextVerify != null) {
					//2.修改下一位审核人记录
					nextVerify.setVerifyStatus(VerifyStatusEnum.PENDING.code());
					applyVerifyMapper.updateChangeStatus(nextVerify);
					//3.通知下一位审核待办
					noticeManager.apply(apply, seal, nextVerify);
				}else {
					//2.变更申请状态
					apply.setVerifyResult(verify.getVerifyResult());
					apply.setApplyStatus(ApplyStatusEnum.PASSED.code());
					apply.setVerifyEndTime(new Date());
					applyMapper.updateChangeStatus(apply);
					//3.添加使用记录
					if(apply.getApplyType().equals(ApplyTypeEnum.TAKE_APPLY.code())) {
						addTakeInfo(apply,seal);
					}else if(apply.getApplyType().equals(ApplyTypeEnum.USED_APPLY.code())) {
						addDevicesAct(apply);
					}
					noticeManager.verify(apply, seal, verify);
				}
			}
		}
	}
	
//	private void verifyUsedApply(Apply apply,ApplyVerify verify) {
//		
//		Long devicesId = apply.getDevicesId();
//		Devices devices = devicesMapper.selectInfoById(devicesId);
//		String workflowType = devices.getWorkflowType();
//		Long workflowId = devices.getWorkflowId();
//		
//		Long partyId = applyVerifyMapper.selectNextVerifyParty(verify.getApplyId(), workflowId,verify.getPartyId());
//		switch (workflowType) {
//		case "QUEUE":
//			
//			if(partyId != null) {
//				//1.变更审核状态
//				applyVerifyMapper.updateChangeStatus(verify);
//				//2.添加下一位审核人记录
//				ApplyVerify nextVerify = new ApplyVerify();
//				nextVerify.setApplyId(apply.getApplyId());
//				nextVerify.setPartyId(partyId);
//				nextVerify.setVerifyStatus(VerifyStatusEnum.PENDING.code());
//				applyVerifyMapper.insert(verify);
//				//3.通知下一位审核待办
//				scheduleManager.add(apply.getCompanyId(), partyId, nextVerify.getApplyVerifyId(), ScheduleTypeEnum.VERIFY.code());
//			}else {
//				//1.变更审核状态
//				applyVerifyMapper.updateChangeStatus(verify);
//				//2.变更申请记录状态
//				apply.setVerifyResult(verify.getVerifyResult());
//				apply.setApplyStatus(ApplyStatusEnum.PASSED.code());
//				apply.setVerifyEndTime(new Date());
//				applyMapper.updateChangeStatus(apply);
//				//3.添加使用记录
//				addDevicesAct(apply);
//				//4.判断是否外带 如果是外带记录
//				if(apply.getApplyType().equals(ApplyTypeEnum.TEST_APPLY.code())) {
//					
//					addTakeInfo(apply);
//				}
//			}
//			break;
//		case "JOIN":
//			
//			
//			if(partyId != null) {
//				
//				//1.变更审核状态
//				applyVerifyMapper.updateChangeStatus(verify);
//				
//				
//			}else {
//				//1.变更审核状态
//				applyVerifyMapper.updateChangeStatus(verify);
//				//2.变更申请记录状态
//				apply.setVerifyResult(verify.getVerifyResult());
//				apply.setApplyStatus(ApplyStatusEnum.PASSED.code());
//				apply.setVerifyEndTime(new Date());
//				applyMapper.updateChangeStatus(apply);
//				
//				//3.添加使用记录
//				addDevicesAct(apply);
//				
//				
//				//4.判断是否外带 如果是外带记录
//				if(apply.getApplyType().equals(ApplyTypeEnum.TEST_APPLY.code())) {
//					
//					addTakeInfo(apply);
//				}
//				
//			}
//
//			break;
//		default:
//			
//			throw new ParametersException("未知审核类型");
//		}
//		
//	}
	private void addDevicesAct(Apply apply) {
		DevicesAct devicesAct = new DevicesAct();
		devicesAct.setSealId(apply.getSealId());
		devicesAct.setApplyId(apply.getApplyId());
		devicesAct.setCompanyId(apply.getCompanyId());
		devicesAct.setSealId(apply.getSealId());
		devicesAct.setDevicesId(apply.getDevicesId());
		devicesAct.setPartyId(apply.getPartyId());
		devicesAct.setTotalTimes(apply.getApplyTimes());//紧急申请最多只能申请一次
		devicesAct.setUseTimes(0);
		devicesAct.setCause(apply.getCause());
		devicesAct.setApplyType(apply.getApplyType());
		devicesAct.setCreateTime(new Date());
		devicesAct.setApplyTime(apply.getApplyTime());
		devicesActMapper.insert(devicesAct);
	}
	
	
	private void addTakeInfo(Apply apply,Seal seal) {
		
		
		Devices devices = devicesMapper.selectInfoById(apply.getDevicesId());
		if(devices == null) {
			throw new ParametersException("设备不存在");
		}
		if(!devices.getTakeStatus().equals(TakeStatusEnum.NORMAL.code())) {
			throw new ParametersException("此设备已被借出");
		}
		
		DevicesTake take = new DevicesTake();
		take.setDevicesId(apply.getDevicesId());
		take.setSealId(apply.getSealId());
		take.setCompanyId(apply.getCompanyId());
		take.setStartDate(apply.getTakeStartTime());
		take.setEndDate(apply.getTakeEndTime());
		take.setTakeStatus(TakeStatusEnum.PENDING.code());
		take.setCause(apply.getCause());
		take.setPartyId(apply.getPartyId());
		take.setApplyId(apply.getApplyId());
		take.setCreateTime(new Date());
		devicesTakeMapper.insert(take);
		
		
		
		
	}
}
